package com.ljm.sign_in.service.impl;

import com.ljm.sign_in.dao.UserDao;
import com.ljm.sign_in.entity.User;
import com.ljm.sign_in.service.UserService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public boolean batchImport(String fileName, MultipartFile file) throws Exception {
        boolean notNull=false;
        List<User> users=new ArrayList<>();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        if(sheet!=null){
            notNull = true;
        }
        for (int r = 1; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            User usera=new User();
            if( row.getCell(0).getCellType() !=1){
                throw new Exception("导入失败(第"+(r+1)+"行,姓名请设为文本格式)");
            }
            String name = row.getCell(0).getStringCellValue();

            if(name == null || name.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,姓名未填写)");
            }
            usera.setName(name);
            usera.setState(0);
            users.add(usera);
        }

        List<User> users1 = userDao.saveAll(users);
        System.out.println(users1);
        return notNull;
    }

    @Override
    public Map<String, Object> list() {
        Map<String,Object> map=new HashMap<>();
        User users=new User();
        users.setState(0);
        List<User> noSignin=userDao.findAll(Example.of(users));
        users.setState(1);
        List<User> sigin=userDao.findAll(Example.of(users));
        map.put("noSignin",noSignin);
        map.put("sigin",sigin);
        map.put("nosum",noSignin.size());
        map.put("insum",sigin.size());
        System.out.println(noSignin);
        System.out.println(sigin);
        return map;
    }

    @Override
    public boolean update(String name) {
        User users=new User();
        users.setName(name);
        List<User> usersList=userDao.findAll(Example.of(users));
        if (usersList.size()==0){
            return false;
        }
        users.set_id(usersList.get(0).get_id());
        users.setState(1);
        userDao.save(users);
        return true;
    }

}
